I recognize, and fully understand, that this data maybe emotionally difficult to work. My intention is to make these lab relevant, allowing you to gather your own insights directly from new visualizations of the data. Please let me know if you would rather not work with the data.

Learning Objectives

gganimate

Tutorials and resources

  1. Maps in R using maps by Eric Anderson
  2. geom_maps
  3. Drawing beautiful maps programmatically with R, sf and ggplot2

Learning Objectives

Creating Country, State and County maps

The ideas for this course tutorial came from multiple examples contributed by Prof. Chris Sunderland and these tutorials

  1. Maps in R using maps by Eric Anderson
  2. geom_maps
  3. Drawing beautiful maps programmatically with R, sf and ggplot2

For our labs two types of approaches will be used to add data to maps. The first that we worked with last week is based on using the longitude and latitude information to add a point at a specific position on a map. The second is two add the information to shapes in a map based on the name of the shape (e.g. states). Although ggmaps is a wonderful tool for mapping using Google Maps and other resources, it is beyond what is needed for now.

As in Lab 9 the sources of data are from Github repo for Novel Coronavirus (COVID-19) Cases that supports the dashboard.

For this lab it is important to note that the time series data does not currently have entries for US States. The daily reports include US State and more recently US country/administrative units. Is possible to concatenate the daily reports to create a time series for US States, but cognizant of changes in the formats of the daily reports.

Building off last weeks examples

From the example from Prof. Chris Sunderland

Here is a graph containing all the coordinate information. Note this is not summarized by country. Since there are now main points for US counties, there are many points in the US

## Warning: Removed 2 rows containing missing values (geom_point).

Zoom in on US 48 states. To do this Alaska, Hawaii and US Territories are filtered. Some US State entries have a Lat and Long of zero, so these are filtered as well.

Here is a prettier version based on the example by Anisa Dhana

## Warning: Transformation introduced infinite values in discrete y-axis

## Warning: Transformation introduced infinite values in discrete y-axis
## Warning in sqrt(x): NaNs produced
## Warning: Removed 1399 rows containing missing values (geom_point).

  • Note that in both examples the ggplot funtion borders is used to define the areas in the map

Mapping data to shapes

Using R color palattes

This is a bit of a digression back to Labs 3 and 4, but there are many R color palattes to choose from or you can create your own. In the above a simple gradient is used. The example from Anisa Dhana uses the viridis palatte which is designed to be perceived by viewers with common forms of colour blindness. Here is an example using a different color package - Wes Anderson. …and more
Top R Color Palettes to Know for Great Data Visualization

If we look at just Massachusetts

## Warning: Transformation introduced infinite values in discrete y-axis

  • Note the cases on Nantucket and Dukes counties were reported as one value and not included on the graph. There is also an asssigned category that includes 303 Confirmed cases as of 3/31/2020.
## # A tibble: 16 x 2
##    Admin2              Confirmed
##    <chr>                   <dbl>
##  1 barnstable                100
##  2 berkshire                 105
##  3 bristol                   129
##  4 dukes                       0
##  5 dukes and nantucket         4
##  6 essex                     350
##  7 franklin                   24
##  8 hampden                    90
##  9 hampshire                  20
## 10 middlesex                 685
## 11 nantucket                   0
## 12 norfolk                   393
## 13 plymouth                  187
## 14 suffolk                   631
## 15 unassigned                303
## 16 worcester                 219

Interactive graphs

In Lab 4 plotly was introduced. It is a great simple way to make interactive graphs

Animated Graphs with gganime

Animated graphs when down right have a great visual impact. You can do this in R and have your animations embedded on your web page. Essentially gganimate creates a series of files that are encompassed in a gif file. In addition to having this gif as part of your report file, you can save the gif and use in a slide or other presentations. It just takes a few lines of code to covert and existing ggplot graph into an animation. See Tutorial for Getting Started with gganimate and gganimate: How to Create Plots with Beautiful Animation in R

This are some important gganimate functions:

  • __transition_*()__ defines how the data should be spread out and how it relates to itself across time.
  • __view_*()__ defines how the positional scales should change along the animation.
  • __shadow_*()__ defines how data from other points in time should be presented in the given point in time.
  • enter_()/exit_() defines how new data should appear and how old data should disappear during the course of the animation.
  • ease_aes() defines how different aesthetics should be eased during transitions.

Preparing the times series data

The time series data is ripe for animation but first we need to get and format the files

time_series_confirmed_long <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")  %>% 
               pivot_longer(-c(Province_State, Country_Region, Lat, Long),
                            names_to = "Date", values_to = "Confirmed") 

# Let's get the times series data for deaths

time_series_deaths_long <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")  %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long),
               names_to = "Date", values_to = "Deaths")

time_series_recovered_long <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region") %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long),
               names_to = "Date", values_to = "Recovered")

# Create Keys 

time_series_confirmed_long <- time_series_confirmed_long %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".", remove = FALSE)

time_series_deaths_long <- time_series_deaths_long %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".") %>% 
  select(Key, Deaths)

time_series_recovered_long <- time_series_recovered_long %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".") %>% 
  select(Key, Recovered)

# Join tables
time_series_long_joined <- full_join(time_series_confirmed_long,
              time_series_deaths_long, by = c("Key"))

time_series_long_joined <- full_join(time_series_long_joined,
              time_series_recovered_long, by = c("Key")) %>% 
    select(-Key)

# Reformat the data
time_series_long_joined$Date <- mdy(time_series_long_joined$Date)

# Create Report table with counts
time_series_long_joined_counts <- time_series_long_joined %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long, Date),
               names_to = "Report_Type", values_to = "Counts")

plotting with gganimate

Below are the packages I installed. There may be others that you need, in particular to rendering gifs. Some of the example may take several minutes to create the animation.

An animation of the confirmed cases in select countries

Animation for Prof. Chris Sunderland’s example

## Warning: Removed 222 rows containing missing values (geom_point).

## Warning: Removed 222 rows containing missing values (geom_point).
## Warning: Removed 214 rows containing missing values (geom_point).

## Warning: Removed 214 rows containing missing values (geom_point).
## Warning: Removed 212 rows containing missing values (geom_point).
## Warning: Removed 209 rows containing missing values (geom_point).

## Warning: Removed 209 rows containing missing values (geom_point).
## Warning: Removed 206 rows containing missing values (geom_point).
## Warning: Removed 203 rows containing missing values (geom_point).

## Warning: Removed 203 rows containing missing values (geom_point).
## Warning: Removed 202 rows containing missing values (geom_point).
## Warning: Removed 199 rows containing missing values (geom_point).
## Warning: Removed 196 rows containing missing values (geom_point).

## Warning: Removed 196 rows containing missing values (geom_point).
## Warning: Removed 192 rows containing missing values (geom_point).
## Warning: Removed 190 rows containing missing values (geom_point).

## Warning: Removed 190 rows containing missing values (geom_point).

## Warning: Removed 190 rows containing missing values (geom_point).

## Warning: Removed 190 rows containing missing values (geom_point).

## Warning: Removed 190 rows containing missing values (geom_point).
## Warning: Removed 189 rows containing missing values (geom_point).

## Warning: Removed 189 rows containing missing values (geom_point).

## Warning: Removed 189 rows containing missing values (geom_point).

## Warning: Removed 189 rows containing missing values (geom_point).
## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).

## Warning: Removed 188 rows containing missing values (geom_point).
## Warning: Removed 187 rows containing missing values (geom_point).

## Warning: Removed 187 rows containing missing values (geom_point).

## Warning: Removed 187 rows containing missing values (geom_point).

## Warning: Removed 187 rows containing missing values (geom_point).

## Warning: Removed 187 rows containing missing values (geom_point).

## Warning: Removed 187 rows containing missing values (geom_point).

## Warning: Removed 187 rows containing missing values (geom_point).
## Warning: Removed 186 rows containing missing values (geom_point).

## Warning: Removed 186 rows containing missing values (geom_point).

## Warning: Removed 186 rows containing missing values (geom_point).
## Warning: Removed 184 rows containing missing values (geom_point).

## Warning: Removed 184 rows containing missing values (geom_point).

## Warning: Removed 184 rows containing missing values (geom_point).

## Warning: Removed 184 rows containing missing values (geom_point).
## Warning: Removed 179 rows containing missing values (geom_point).

## Warning: Removed 179 rows containing missing values (geom_point).
## Warning: Removed 175 rows containing missing values (geom_point).
## Warning: Removed 168 rows containing missing values (geom_point).

## Warning: Removed 168 rows containing missing values (geom_point).
## Warning: Removed 164 rows containing missing values (geom_point).
## Warning: Removed 157 rows containing missing values (geom_point).

## Warning: Removed 157 rows containing missing values (geom_point).
## Warning: Removed 152 rows containing missing values (geom_point).
## Warning: Removed 147 rows containing missing values (geom_point).
## Warning: Removed 139 rows containing missing values (geom_point).

## Warning: Removed 139 rows containing missing values (geom_point).
## Warning: Removed 135 rows containing missing values (geom_point).
## Warning: Removed 127 rows containing missing values (geom_point).

## Warning: Removed 127 rows containing missing values (geom_point).
## Warning: Removed 123 rows containing missing values (geom_point).
## Warning: Removed 114 rows containing missing values (geom_point).

## Warning: Removed 114 rows containing missing values (geom_point).
## Warning: Removed 111 rows containing missing values (geom_point).
## Warning: Removed 106 rows containing missing values (geom_point).
## Warning: Removed 102 rows containing missing values (geom_point).

## Warning: Removed 102 rows containing missing values (geom_point).
## Warning: Removed 97 rows containing missing values (geom_point).
## Warning: Removed 89 rows containing missing values (geom_point).

## Warning: Removed 89 rows containing missing values (geom_point).
## Warning: Removed 87 rows containing missing values (geom_point).
## Warning: Removed 73 rows containing missing values (geom_point).

## Warning: Removed 73 rows containing missing values (geom_point).
## Warning: Removed 58 rows containing missing values (geom_point).
## Warning: Removed 52 rows containing missing values (geom_point).
## Warning: Removed 45 rows containing missing values (geom_point).

## Warning: Removed 45 rows containing missing values (geom_point).
## Warning: Removed 42 rows containing missing values (geom_point).
## Warning: Removed 37 rows containing missing values (geom_point).

## Warning: Removed 37 rows containing missing values (geom_point).
## Warning: Removed 31 rows containing missing values (geom_point).
## Warning: Removed 22 rows containing missing values (geom_point).

## Warning: Removed 22 rows containing missing values (geom_point).
## Warning: Removed 20 rows containing missing values (geom_point).
## Warning: Removed 15 rows containing missing values (geom_point).

## Warning: Removed 15 rows containing missing values (geom_point).

## Warning: Removed 15 rows containing missing values (geom_point).
## Warning: Removed 13 rows containing missing values (geom_point).
## Warning: Removed 10 rows containing missing values (geom_point).

## Warning: Removed 10 rows containing missing values (geom_point).
## Warning: Removed 7 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 3 rows containing missing values (geom_point).

## Warning: Removed 3 rows containing missing values (geom_point).

## Warning: Removed 3 rows containing missing values (geom_point).
## Warning: Removed 2 rows containing missing values (geom_point).

Exercises

For Lab 10 created a report with static maps, interactive graphs and animations that is meant to be read by others. Hide warnings, messages and even the code you used so that it is readable. Included references. Link to the Lab 10 report from your Github site. Submit the link to Moodle.